package com.sdy.usercenter.web.controller;

import com.sdy.auth.api.AuthApi;
import com.sdy.auth.api.model.AuthDto;
import com.sdy.auth.api.model.UserDto;
import com.sdy.auth.api.model.UserInfo;
import com.sdy.auth.client.service.SsoService;
import com.sdy.common.model.Response;
import com.sdy.common.utils.Assert;
import com.sdy.common.utils.DesensitizeUtil;
import com.sdy.common.utils.EncodeUtil;
import com.sdy.mvc.controller.BaseController;
import com.sdy.redis.service.RedisService;
import com.sdy.usercenter.biz.constants.RedisConstants;
import com.sdy.usercenter.biz.model.SysUser;
import com.sdy.usercenter.biz.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.KeyPair;
import java.util.Base64;
import java.util.Map;

/**
 * Created by zzq on 2019/1/10.
 */
@Slf4j
@RestController
public class IndexController extends BaseController {
    @Autowired
    private RedisService redisService;
    @Autowired
    private SsoService ssoService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private AuthApi authApi;
    @Value("${res.app.code}")
    private String appCode;

    @GetMapping("/publicKey")
    public Response publicKey(HttpServletRequest request) throws Exception {
        KeyPair keyPair = EncodeUtil.rsaGenKeyPair();
        String publicKeyStr = new String(Base64.getEncoder().encode(keyPair.getPublic().getEncoded()));
        redisService.set(RedisConstants.REDIS_PREFIX + "privateKey_" + request.getSession().getId(), Base64.getEncoder().encodeToString(keyPair.getPrivate().getEncoded()), 20);
        return Response.success(publicKeyStr);
    }

    @PostMapping("/modifyPassword")
    public Response modifyPassword(HttpServletRequest request, @RequestBody Map<String, String> params) throws Exception {
        String passwordOld = params.get("passwordOld");
        String passwordNew = params.get("password");
        UserInfo userInfo = ssoService.getUserInfo(request);
        Assert.isNull(userInfo.getUserId(), "用户id不能为空");
        Assert.isBlank(passwordNew, "新密码不能为空");
        SysUser user = sysUserService.getById(userInfo.getUserId());
        Assert.isTrue(!user.getPassword().equals(passwordOld), "密码错误");
        sysUserService.modifyPassword(userInfo.getUserId(), passwordNew);
        return Response.success();
    }

    @GetMapping("/userInfo")
    public Response userInfo(HttpServletRequest request) {
        request.getHeader("referer");
        Integer userId = ssoService.getUserInfo(request).getUserId();
        UserDto user = authApi.userInfo(userId).getData();
        user.setIdCard(DesensitizeUtil.hideIdCardNo(user.getIdCard()))
                .setPhone(DesensitizeUtil.hideCellPhone(user.getPhone()));
        return Response.success(user);
    }

    @GetMapping("/authMap")
    public Response authMap(HttpServletRequest request) {
        Integer userId = ssoService.getUserId(request);
        Map<String, Boolean> authMap = authApi.getAuthTree(appCode, userId).convertToMap();
        return Response.success(authMap);
    }
}
